package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Account;
import com.sl.au.entity.BusinesssellerAuthentication;
import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.Evaluate;
import com.sl.au.entity.Option;
import com.sl.au.entity.User;
import com.sl.au.repository.BusinesssellerBaseInfoRepository;
import com.sl.au.repository.BussinessLevelRepository;
import com.sl.au.repository.EvaluateRepository;
import com.sl.au.repository.OptionRepository;
import com.sl.au.repository.UserRepository;
import com.sl.au.repository.AccountRepository;
import com.sl.au.repository.BusinesssellerAuthenticationRepository;
import com.sl.au.service.BusinesssellerBaseInfoService;

@Service
@Transactional
public class BusinesssellerBaseInfoServiceImpl extends BaseServiceImpl implements BusinesssellerBaseInfoService {

	@Autowired
	private BusinesssellerBaseInfoRepository businesssellerBaseInfoRepository;

	@Autowired
	private BusinesssellerAuthenticationRepository businesssellerAuthenticationRepository;
	@Autowired
	private AccountRepository accountRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private OptionRepository optionRepository;
	@Autowired
	private EvaluateRepository evaluateRepository;

	@Override
	public BusinesssellerBaseInfoRepository getRepository() {
		return this.businesssellerBaseInfoRepository;
	}

	@Override
	public void saveMany(User user, BusinesssellerBaseInfo b, Account account) {
		// 先保存用户的相关信息
		userRepository.saveAndFlush(user);

		// 设置商户的其他信息
		b.setManagementPerson(user);
		List<Option> options = optionRepository.findByGroupAndTitle("信息状态", "认证通过");
		b.setInfoState(options.get(0));
		// b.setBussinessLevel(user.getUserLevel());
		// 保存商户信息
		b.setCodeAdress("/ui/bussinessmain?device=mobile&&identify=" + b.getId());
		businesssellerBaseInfoRepository.saveAndFlush(b);
		// 保存账户信息
		account.setIsEnabled(true);
		account.setUser(user);
		accountRepository.save(account);
		// 关联认证信息
		BusinesssellerAuthentication au = businesssellerAuthenticationRepository.findByShopName(b.getShopName());
		if (au == null) {
			au = new BusinesssellerAuthentication();
		}

		au.setManagementPerson(user);
		au.setShopAdress(b.getShopAdress());
		au.setShopName(b.getShopName());
		au.setAreaMeasure(b.getAreaMeasure());
		au.setLongitude(b.getLongitude());
		au.setLatitude(b.getLatitude());
		au.setCleaningPerson(b.getCleaningPerson());
		au.setSignPerson(b.getSignPerson());
		au.setCheckDate(new Date());
		au.setManagementRange(b.getManagementRange());
		au.setMemo(b.getMemo());
		au.setBusinesssellerBaseInfo(b);
		au.setInfoState(optionRepository.findByGroupAndTitle("信息状态", "认证通过").get(0));
		businesssellerAuthenticationRepository.save(au);
		// 关联评估信息
		Evaluate evaluate = evaluateRepository.getInfoByBusinessseller(b.getId());
		if (evaluate == null) {
			evaluate = new Evaluate();
		}
		evaluate.setBusinesssellerBaseInfo(b);
		evaluate.setEvaluateScale(100);
		evaluateRepository.save(evaluate);
	}

	@Override
	public int savepicture(String url, String name) {
		int code = -1;
		BusinesssellerBaseInfo binfo = this.businesssellerBaseInfoRepository.getInfoByName(name);
		if (binfo != null) {
			BusinesssellerAuthentication ba = this.businesssellerAuthenticationRepository
					.getInfoByBaseInfoId(binfo.getId());
			binfo.setPictureAdress(url);
			this.businesssellerBaseInfoRepository.save(binfo);
			if (ba != null) {
				ba.setPictureAdress(url);
				this.businesssellerAuthenticationRepository.save(ba);
			}
		}
		code = 1;

		return code;
	}

	@Override
	public List<BusinesssellerBaseInfo> getPage(int start, int length, String key, String orderName, Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.businesssellerBaseInfoRepository.getByKey(key, pageable);
		} else {
			pages = businesssellerBaseInfoRepository.findAll(pageable);
		}

		return pages.getContent();
	}

	@Override
	public List<BusinesssellerBaseInfo> getPage(int start, int length, String key, String level, String orderName,
			Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key) && StringUtils.hasText(level)) {
			pages = this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, pageable);
		} else if (StringUtils.hasText(key) && !StringUtils.hasText(level)) {
			pages = this.businesssellerBaseInfoRepository.getByKey(key, pageable);
		} else if (!StringUtils.hasText(key) && StringUtils.hasText(level)) {
			pages = this.businesssellerBaseInfoRepository.getByLevel(level, pageable);
		} else {
			pages = this.businesssellerBaseInfoRepository.findAll(pageable);
		}

		return pages.getContent();
	}

	@Override
	public List<BusinesssellerBaseInfo> getPageByLevel(int start, int length, String key, String level,
			String orderName, Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, pageable);
		} else {
			pages = this.businesssellerBaseInfoRepository.getByLevel(level, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCount(String key) {
		if (!StringUtils.hasText(key))
			return this.businesssellerBaseInfoRepository.findAll().size();
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		return businesssellerBaseInfoRepository.getByKey(key, pageable).getContent().size();
	}

	@Override
	public int getCount(String key, String level) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key) && StringUtils.hasText(level)) {
			return this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, pageable).getContent().size();
		} else if (StringUtils.hasText(key) && !StringUtils.hasText(level)) {
			return this.businesssellerBaseInfoRepository.getByKey(key, pageable).getContent().size();
		} else if (!StringUtils.hasText(key) && StringUtils.hasText(level)) {
			return this.businesssellerBaseInfoRepository.getByLevel(level, pageable).getContent().size();
		} else {
			return this.businesssellerBaseInfoRepository.findAll(pageable).getContent().size();
		}
	}

	@Override
	public int getCountByLevel(String key, String level) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			return this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, pageable).getContent().size();
		} else {
			return this.businesssellerBaseInfoRepository.getByLevel(level, pageable).getContent().size();
		}
	}

	@Override
	public List<BusinesssellerBaseInfo> findAll() {

		return businesssellerBaseInfoRepository.findAll();
	}

	@Override
	public BusinesssellerBaseInfo findByBussinessSellerId(String bussinessId) {

		return businesssellerBaseInfoRepository.findOne(bussinessId);
	}

	@Override
	public List<BusinesssellerBaseInfo> getPageByNoLevel(int start, int length, String key, String orderName,
			Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.businesssellerBaseInfoRepository.getNoLevelByKey(key, pageable);
		} else {
			pages = businesssellerBaseInfoRepository.findNoLevelAll(pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByNoLevel(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			return this.businesssellerBaseInfoRepository.getNoLevelByKey(key, pageable).getContent().size();
		} else {
			return this.businesssellerBaseInfoRepository.findNoLevelAll(pageable).getContent().size();
		}
	}

	@Override
	public void deleteBussinessLevel(String[] ids) {
		if (ids.length > 0) {
			for (String bussinessLevelId : ids) {
				businesssellerBaseInfoRepository.setBusinesssellerBaseInfo(bussinessLevelId);
			}
		}

	}

	@Override
	public BusinesssellerBaseInfo findByShopName(String name) {

		return businesssellerBaseInfoRepository.findByShopName(name);
	}

	@Override
	public List<BusinesssellerBaseInfo> getPage(int start, int length, String key, String level, String role,
			String orderName, Direction dir) {
		
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key) && StringUtils.hasText(level)) {
			pages = this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, role, pageable);
		} else if (StringUtils.hasText(key) && !StringUtils.hasText(level)) {
			pages = this.businesssellerBaseInfoRepository.getByKey(key, role, pageable);
		} else if (!StringUtils.hasText(key) && StringUtils.hasText(level)) {
			pages = this.businesssellerBaseInfoRepository.getByLevel(level, role, pageable);
		} else {
			pages = this.businesssellerBaseInfoRepository.findAll(role,pageable);
		}
		
		return pages.getContent();
	}

	@Override
	public int getCount(String key, String level, String role) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key) && StringUtils.hasText(level)) {
			return this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, role, pageable).getContent().size();
		} else if (StringUtils.hasText(key) && !StringUtils.hasText(level)) {
			return this.businesssellerBaseInfoRepository.getByKey(key, role, pageable).getContent().size();
		} else if (!StringUtils.hasText(key) && StringUtils.hasText(level)) {
			return this.businesssellerBaseInfoRepository.getByLevel(level, role, pageable).getContent().size();
		} else {
			return this.businesssellerBaseInfoRepository.findAll(role,pageable).getContent().size();
		}
	}

	@Override
	public List<BusinesssellerBaseInfo> getPageByNoLevel(int start, int length, String key, String role,
			String orderName, Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.businesssellerBaseInfoRepository.getNoLevelByKey(key, role, pageable);
		} else {
			pages = businesssellerBaseInfoRepository.findNoLevelAll(role, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByNoLevel(String key, String role) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			return this.businesssellerBaseInfoRepository.getNoLevelByKey(key, role, pageable).getContent().size();
		} else {
			return this.businesssellerBaseInfoRepository.findNoLevelAll(role, pageable).getContent().size();
		}
	}

	@Override
	public List<BusinesssellerBaseInfo> getPageByLevel(int start, int length, String key, String level, String role,
			String orderName, Direction dir) {
		Page<BusinesssellerBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, role, pageable);
		} else {
			pages = this.businesssellerBaseInfoRepository.getByLevel(level, role, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByLevel(String key, String level, String role) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			return this.businesssellerBaseInfoRepository.getByKeyAndLevel(key, level, role,pageable).getContent().size();
		} else {
			return this.businesssellerBaseInfoRepository.getByLevel(level, role, pageable).getContent().size();
		}
	}

}
